
#include <BStyle>
#include <BPainter>
#include <BRender>
#include <BViewer>
#include <BScene>
#include <BCone>
#include <BCube>
#include <BImage>
#include <BMaterial>
#include <BReadWrite>

#include "FixedHandler.h"

FixedHandler::FixedHandler()
{
	_lengthText = new BTextual();
	_lengthText->setColor(200, 0, 0);
	_lengthText->setCullback(false);
	_lengthText->setLighting(false);
	_lengthText->setPolyMode(Poly_Fill);
	_lengthText->setGlyphSize((BReal)0.3);

	_nodeA = new BBody("nodeA");
	{
		BCube* cube = new BCube(0.4, 12);
		cube->setColor(100, 200, 255);
		_nodeA->insertShape(cube);
		_nodeA->setMass(1);
	}

	_nodeB = new BBody("nodeB");
	{
		BCone* cone = new BCone(0.05, 0.2, 24, 10);
		cone->setColor(200, 100, 0);
		_nodeB->insertShape(cone);
		_nodeB->setMass(1);
	}

	BVector pointA = _nodeA->space().max() + BVector(0.1, 0, 0);
	BVector pointB(0, 0, 0);
	_restraint = new BFixedRestraint(_nodeA, pointA, _nodeB, pointB);
	_restraint->setColliding(false);
	_restraint->setName("Conear");
}
FixedHandler::~FixedHandler()
{

}

void FixedHandler::attached(BViewer* viewer)
{
	BScene* dscene = viewer->scene();
	_nodeA->setPosition(0, 0, 5);
	_nodeB->setPosition(2, 2, 5);
	dscene->insert(_nodeA);
	dscene->insert(_nodeB);
	dscene->insert(_restraint);
	_lengthText->setFont((BFont*)viewer->realFont());
	viewer->scene()->insert(_lengthText);
	this->emit("edit-restraint", _restraint);
}
void FixedHandler::detached(BViewer* viewer)
{
	BScene* dscene = viewer->scene();
	dscene->remove(_nodeA);
	dscene->remove(_nodeB);
	dscene->remove(_restraint);
	viewer->scene()->remove(_lengthText);
}

void FixedHandler::paint(BPainter& painter)
{
	BViewer* viewer = this->viewer();
}
void FixedHandler::render(BRender& render)
{
	render.setLighting(false);
	render.setDepthTest(false);
	render.setLineSmooth(true);
	render.setLineWidth(1);

	if (BNode* nodeA = _restraint->bodyA())
	{
		const BMatrix& matrixA = nodeA->matrix();
		BVector origin = matrixA.position();
		BVector axisX = matrixA.mult3(1, 0, 0);
		BVector axisY = matrixA.mult3(0, 1, 0);
		BVector axisZ = matrixA.mult3(0, 0, 1);

		render.setColor(200, 0, 0);
		render.drawLine(origin, origin + axisX);
		render.setColor(0, 200, 0);
		render.drawLine(origin, origin + axisY);
		render.setColor(0, 0, 200);
		render.drawLine(origin, origin + axisZ);
	}

	if (BNode* nodeB = _restraint->bodyB())
	{
		const BMatrix& matrixB = nodeB->matrix();
		BVector origin = matrixB.position();
		BVector axisX = matrixB.mult3(1, 0, 0);
		BVector axisY = matrixB.mult3(0, 1, 0);
		BVector axisZ = matrixB.mult3(0, 0, 1);

		render.setColor(200, 0, 0);
		render.drawLine(origin, origin + axisX);
		render.setColor(0, 200, 0);
		render.drawLine(origin, origin + axisY);
		render.setColor(0, 0, 200);
		render.drawLine(origin, origin + axisZ);

		BVector axis0 = _restraint->query("debug-axis0");
		BVector axis1 = _restraint->query("debug-axis1");
		BVector axis2 = _restraint->query("debug-axis2");
		render.setLineWidth(2);
		render.setLineStipple(0x0f0f);
		render.setColor(200, 0, 0);
		render.drawLine(origin, origin + axis0);
		render.setColor(0, 200, 0);
		render.drawLine(origin, origin + axis1);
		render.setColor(0, 0, 200);
		render.drawLine(origin, origin + axis2);
	}
}

void FixedHandler::styleEvent(const BEvent& event)
{
	BHandler::styleEvent(event);
	const BStyle* style = event.value();
	BImage* image = style->image("cone-texture");
	if (BShape* shape = _nodeB->shape(0))
	{
		shape->setTexture(image);
	}
}

